package dao

import (
	"context"
	"encoding/json"
	"fmt"
	"petCombo-srv/global"
	"petCombo-srv/internal/model"
	__ "petCombo-srv/proto"
)

type ComboImpl interface {
	ComboList() ([]*__.ComboList, error)
}
type ComboListService struct {
}

func NewComboListService() *ComboListService {
	return &ComboListService{}
}
func (c *ComboListService) ComboList(comboId int) ([]*__.ComboList, error) {
	var combo model.Combo

	key := fmt.Sprintf("combo_list")

	result, err := global.Rdb.Get(context.Background(), key).Result()
	if err != nil {
		return nil, err
	}

	err = json.Unmarshal([]byte(result), &combo)
	if err != nil {
		return nil, err
	}
	var list []*__.ComboList
	list = append(list, &__.ComboList{
		Title:  combo.Title,
		Name:   combo.Name,
		TypeId: int64(combo.TypeId),
		Price:  float32(combo.Price),
	})
	if list != nil {
		return list, nil
	}

	List := combo.ComboList(global.DB, comboId)

	bytes, err := json.Marshal(List)
	if err != nil {
		return nil, err
	}

	err = global.Rdb.Set(context.Background(), key, bytes, 0).Err()
	if err != nil {
		return nil, err
	}

	return List, nil

}
